package com.yami.shop.api.controller;

import cn.hutool.core.util.StrUtil;
import com.yami.shop.security.api.model.YamiUser;
import com.yami.shop.security.common.bo.UserInfoInTokenBO;
import com.yami.shop.security.common.manager.TokenStore;
import com.yami.shop.security.common.util.AuthUserContext;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyToken {
    @Value("${sa-token.token-name}")
    private String tokenName;

    @Autowired
    private  TokenStore tokenStore;
    public Integer getMallId(HttpServletRequest req) {
        String accessToken = req.getHeader(tokenName);
        if(StrUtil.isNotBlank(accessToken)) {
            UserInfoInTokenBO userInfoInToken = tokenStore.getUserInfoByAccessToken(accessToken, true);
            if (userInfoInToken != null) {
                return userInfoInToken.getMallId();
            }
        }

        return 0;


    }

}
